var wxpay = require('../../utils/pay.js')
var app = getApp()
Page({
  data:{
    statusType: ["待付款", "待发货", "待收货", "待评价", "已完成"],
    currentType:0,
    tabClass: ["", "", "", "", ""]
  },
  statusTap:function(e){
     var curType =  e.currentTarget.dataset.index;
     this.data.currentType = curType
     this.setData({
       currentType:curType
     });
     this.onShow();
  },
  orderDetail : function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/order-details/index?id=" + orderId
    })
  },
  cancelOrderTap:function(e){
    var that = this;
    var orderId = e.currentTarget.dataset.id;
     wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function(res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url: app.globalData._path + app.globalData.subDomain + '/order/close',
            data: {
              token: wx.getStorageSync('token'),
              orderId: orderId
            },
            success: (res) => {
              wx.hideLoading();
              if (res.data.code == 0) {
                that.onShow();
              }
            }
          })
        }
      }
    })
  },
  toPayTap:function(e){
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var money = e.currentTarget.dataset.money;
    wxpay.wxpay(app, money, orderId, "/pages/order-list/index");   
  },
  toConfirm:function(e){
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.showLoading();
    wx.request({
      url: app.globalData._path + app.globalData.subDomain + '/order/confirm.order',
      data:{
        orderid: orderId
      },
      success:function(res){
        wx.hideLoading();
        if(res.data.code == 0){
          wx.showModal({
            title: '提示',
            content: '确认收货成功',
            showCancel:false,
            complete:function(){
              that.onShow();
            }
          });
          
        }else{
          wx.showModal({
            title: '错误提示',
            content: res.data.list,
            showCancel: false
          })
          return ;
        }
      }
    })
  },
  toComment: function (e) {
    wx.redirectTo({
      url: '/pages/order-details/index?id=' + e.currentTarget.dataset.id,
    })
  },
  toOrderDetail: function (e) {
    wx.redirectTo({
      url: '/pages/order-details/index?id=' + e.currentTarget.dataset.id,
    })
  },
  onLoad:function(options){
    // 生命周期函数--监听页面加载
    if(options.type){
      this.setData({
        currentType: options.type
      });
    }
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
  
  },
  onShow:function(){
    // 获取订单列表
    wx.showLoading();
    var that = this;
    var postData = {
      token: wx.getStorageSync('token'),
      uid: wx.getStorageSync('uid')
    };
    postData.status = that.data.currentType;
    wx.request({
      url: app.globalData._path + app.globalData.subDomain + '/order/get.order',
      data: postData,
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 0) {
          that.setData({
            orderList: res.data.list.data
          });
        } else {
          this.setData({
            orderList: null
          });
        }
      }
    })
    
  },
  onHide:function(){
    // 生命周期函数--监听页面隐藏
 
  },
  onUnload:function(){
    // 生命周期函数--监听页面卸载
 
  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作
   
  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数
  
  }
})
